/*jslint sloppy: true */
/*global require */
var angular = require('angular');
var LM = require('../global');
require('./login.html');

LM.controller('Login', function ($rootScope, $scope, Account, $location) {
	var $captcha = '';
	$scope.queryConfig.clear().setActive();
	$scope.navConfig.setHref('#/').setType('home');

	$scope.loginBody = new Account();
	$scope.login = function () {
		$scope.loginBody.$login({
			captcha: $captcha
		}, function () {
			$rootScope.account = $scope.loginBody;
			$location.url('/project');
		}, function (response) {
			$scope.exceptionMessage = response.data.msg;
		});
	};

	$scope.captcha = function (newValue) {
		if (angular.isDefined(newValue)) {
			$captcha = newValue.substr(0, 4);
		}
		return $captcha;
	};

	//init
	$scope.getNow();
});
